﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Windows.Input;
using DAL;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using TripAssistantDesktop.Modules;
using TripAssistantDesktop.TripAssistantWCFService;


namespace TripAssistantDesktop.ViewModel
{
    internal interface ITripsNavigationViewModel
    {
        ObservableCollection<Trip> Trips { get; set; }
    }

    class TripsNavigationViewModel : ViewModelBase, ITripsNavigationViewModel
    {
        private ObservableCollection<Trip> _trips = new ObservableCollection<Trip>();
        private IAssistantService _srv; // = new ChannelFactory<IAssistantService>().CreateChannel();
        public ICommand SelectTrip { get; set; }
        private IUnityContainer _container;
        private IEventAggregator _eventAggregator; 
        public ObservableCollection<Trip> Trips
        {
            get { return _trips; }
            set { _trips = value; }
        }

        public Trip SelectedTrip
        {
            get { return _selectedTrip; }
            set { 
                _selectedTrip = value;
            _eventAggregator.GetEvent<SelectedTripChangedEvent>().Publish(value);
            }
        }

        private Trip _selectedTrip; 

        public TripsNavigationViewModel(IUnityContainer container, IEventAggregator eventAggregator, IAssistantService srv)
        {
            _srv = srv; 
            _srv.GetTrips(new User(){FirstName = "a", LastName = "d"}).ToList().ForEach(x=> _trips.Add(x));
            SelectTrip = new DelegateCommand<Trip>(SelectTripExecute, SelectTripCanExecute);
            _container = container;
            _eventAggregator = eventAggregator; 
        }
        private bool SelectTripCanExecute(Trip trip)
        {
            return true;
            //throw new NotImplementedException();
        }

        private void SelectTripExecute(Trip trip)
        {
            var rm = _container.Resolve<IRegionManager>(); 
            //_container.Resolve<IOrdersViewModel>().DataSource = _srv.GetOrders(trip); 
            _eventAggregator.GetEvent<SelectedTripChangedEvent>().Publish(trip);
            //throw new NotImplementedException();
        }
    }
}
